package com.itheima.reggie.controller;

import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

@RestController//员共管理模块
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //员工登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Map<String, String> param, HttpSession session) {
        //接受请求参数
        String username = param.get("username");
        String password = param.get("password");
        //2调用service
        ResultInfo resultInfo = employeeService.login(username, password);
        //判断登陆状态
        if (resultInfo.getCode() == 1) {
            //登录成功将用户信息存储到session中
            session.setAttribute(Constant.SESSION_EMPLOYEE, resultInfo.getData());

        }
        //返回结果
        return resultInfo;
    }

    //员工退出
    @PostMapping("employee/logout")
    public ResultInfo logout(HttpSession session) {
        //清理session
        session.invalidate();
        //返回结果
        return ResultInfo.success();
    }

    //员工列表
    @GetMapping("employee/find")
    public ResultInfo find(String name) {
        //调用service
        List<Employee> employeeList = employeeService.find(name);
        //返回结果
        return ResultInfo.success(employeeList);
    }


    //新增员工
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee) {
        //调用service
        employeeService.save(employee);
        //返回结果
        return ResultInfo.success();
    }

    //回显员工
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        //调用service
        Employee employee = employeeService.findById(id);
        //返回结果
        return ResultInfo.success(employee);
    }

    //修改员工
    @PutMapping("employee")
    public ResultInfo update(@RequestBody Employee employee) {//接受put参数
        //调用serv
        employeeService.update(employee);
        //返回结果
        return ResultInfo.success();
    }
}
